package com.heart.admin.config;

import com.heart.common.constant.AppCode;
import com.heart.common.constant.AppConstant;
import com.heart.common.exception.ValidatorException;
import com.heart.common.exception.WorkingException;
import com.heart.dao.dto.RequestResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
@ResponseBody
@Slf4j
public class GlobalException {
    @ExceptionHandler(value = Exception.class)
    public RequestResult workingException(HttpServletRequest request, Exception e) {
        log.error(e.getMessage(),e);
        if (e instanceof WorkingException) {
            return new RequestResult(AppCode.ERROR, AppConstant.EMPTY,e.getMessage());
        } else if (e instanceof ValidatorException){
            return new RequestResult(AppCode.PARAM_ERROR, AppConstant.EMPTY,e.getMessage());
        }
        return new RequestResult(AppCode.ERROR, AppConstant.EMPTY, e.getMessage());
    }
}
